package gui;

import dados.Questao;
import dados.Teste;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import logica.Arquivos;
import logica.GerenciaSerializacao;
import gui.modelos.KeyListenerJanela;

public class JanelaQuestao extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;
	private Questao questao = null;
    private boolean modificado = false;
    
    public JanelaQuestao() {
        super();
        System.out.println("Elaborar Questão - gui.JanelaQuestao");
        initComponents();
        Janelas.alinharContainer(this);
        inicializarJOptionPane();
        this.setResizable(true);
        this.addKeyListener(new KeyListenerJanela());
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowListener() {
        	public void windowOpened(WindowEvent e) {	}

            public void windowClosing(WindowEvent e) {
                if(modificado){
                    int opcao = JOptionPane.showConfirmDialog(null, "Existem alterações não salvas. Deseja sair assim mesmo?", "Atenção!", JOptionPane.OK_CANCEL_OPTION);
                    if(opcao == JOptionPane.OK_OPTION)
                        dispose();
                } else {
                    dispose();
                }
            }

            public void windowClosed(WindowEvent e) {	}

            public void windowIconified(WindowEvent e) {	}

            public void windowDeiconified(WindowEvent e) {	}

            public void windowActivated(WindowEvent e) {	}

            public void windowDeactivated(WindowEvent e) {	}
        });
        
        questao = new Questao();
        textEnunciado.setEditable(true);
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(new JTextArea());
        scrollPane.setEnabled(true);
        textEnunciado.add(scrollPane);
        rdPadraoEntrada.setSelected(true);
        txtArquivoEntrada.setText("");
        txtArquivoEntrada.setEnabled(false);
        rdPadraoSaida.setSelected(true);
        txtArquivoSaida.setText("");
        txtArquivoSaida.setEnabled(false);
        questao.setNomeArquivoSaida(null);
        btnRemoverTeste.setEnabled(false);
        btnEditarTeste.setEnabled(false);
    }
    
    private void inicializarJOptionPane() {
        UIManager.put("OptionPane.yesButtonText", "Sim");
        UIManager.put("OptionPane.noButtonText", "Não");
        UIManager.put("OptionPane.cancelButtonText", "Cancelar");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
    	buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        textEnunciado = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        rdPadraoSaida = new javax.swing.JRadioButton();
        rdArquivoSaida = new javax.swing.JRadioButton();
        txtArquivoSaida = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        listaTestes = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        btnAdicionarTeste = new javax.swing.JButton();
        btnAdicionarTesteTXT = new javax.swing.JButton();
        btnRemoverTeste = new javax.swing.JButton();
        btnEditarTeste = new javax.swing.JButton();
        btnAbrirQuestao = new javax.swing.JButton();
        btnSalvarQuestao = new javax.swing.JButton();
        btnAdicionarEnunciado = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        textGabarito = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        btnAdicionarGabarito = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        rdPadraoEntrada = new javax.swing.JRadioButton();
        rdArquivoEntrada = new javax.swing.JRadioButton();
        txtArquivoEntrada = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        textEnunciado.setColumns(20);
        textEnunciado.setRows(5);
        textEnunciado.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textEnunciadoKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(textEnunciado);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo de Saída"));

        buttonGroup1.add(rdPadraoSaida);
        rdPadraoSaida.setText("Padrão");
        rdPadraoSaida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdPadraoSaidaActionPerformed(evt);
            }
        });

        buttonGroup1.add(rdArquivoSaida);
        rdArquivoSaida.setText("Arquivo");
        rdArquivoSaida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdArquivoSaidaActionPerformed(evt);
            }
        });

        txtArquivoSaida.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtArquivoSaidaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rdPadraoSaida)
                    .addComponent(rdArquivoSaida)
                    .addComponent(txtArquivoSaida, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(rdPadraoSaida)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rdArquivoSaida)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtArquivoSaida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setText("Enunciado");

        jScrollPane3.setViewportView(listaTestes);

        jLabel5.setText("Testes");

        btnAdicionarTeste.setText("Adicionar teste");
        btnAdicionarTeste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarTesteActionPerformed(evt);
            }
        });

        btnAdicionarTesteTXT.setText("Teste Arquivo");
        btnAdicionarTesteTXT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	btnAdicionarTesteTxtActionPerformed(evt);
            }
        });
        
        btnRemoverTeste.setText("Remover teste");
        btnRemoverTeste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoverTesteActionPerformed(evt);
            }
        });

        btnEditarTeste.setText("Editar teste");
        btnEditarTeste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarTesteActionPerformed(evt);
            }
        });

        btnAbrirQuestao.setText("Abrir Questão");
        btnAbrirQuestao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbrirQuestaoActionPerformed(evt);
            }
        });

        btnSalvarQuestao.setText("Salvar Questão");
        btnSalvarQuestao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarQuestaoActionPerformed(evt);
            }
        });

        btnAdicionarEnunciado.setText("Adicionar/Modificar");
        btnAdicionarEnunciado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarEnunciadoActionPerformed(evt);
            }
        });

        textGabarito.setColumns(20);
        textGabarito.setRows(5);
        textGabarito.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textGabaritoKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(textGabarito);

        jLabel2.setText("Programa Gabarito");

        btnAdicionarGabarito.setText("Adicionar/Modificar");
        btnAdicionarGabarito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarGabaritoActionPerformed(evt);
            }
        });

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo de Entrada"));

        buttonGroup2.add(rdPadraoEntrada);
        rdPadraoEntrada.setText("Padrão");
        rdPadraoEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdPadraoEntradaActionPerformed(evt);
            }
        });

        buttonGroup2.add(rdArquivoEntrada);
        rdArquivoEntrada.setText("Arquivo");
        rdArquivoEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdArquivoEntradaActionPerformed(evt);
            }
        });

        txtArquivoEntrada.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtArquivoEntradaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rdPadraoEntrada)
                    .addComponent(rdArquivoEntrada)
                    .addComponent(txtArquivoEntrada, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addComponent(rdPadraoEntrada)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rdArquivoEntrada)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtArquivoEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(btnAdicionarEnunciado))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(btnAdicionarGabarito)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(btnAbrirQuestao, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                    .addComponent(btnSalvarQuestao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAdicionarTeste)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            	.addComponent(btnAdicionarTesteTXT, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnEditarTeste, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnRemoverTeste, javax.swing.GroupLayout.Alignment.LEADING)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAbrirQuestao)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 319, Short.MAX_VALUE)
                        .addComponent(btnSalvarQuestao))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnAdicionarTeste)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAdicionarTesteTXT)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnRemoverTeste)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnEditarTeste)
                                .addGap(35, 35, 35))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jPanel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAdicionarEnunciado)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAdicionarGabarito)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rdPadraoSaidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdPadraoSaidaActionPerformed
        txtArquivoSaida.setText("");
        txtArquivoSaida.setEnabled(false);
        questao.setNomeArquivoSaida(null);
        this.modificado = true;
    }//GEN-LAST:event_rdPadraoSaidaActionPerformed

    private void rdArquivoSaidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdArquivoSaidaActionPerformed
        txtArquivoSaida.setEnabled(true);
        questao.setNomeArquivoSaida(txtArquivoSaida.getText());
        this.modificado = true;
    }//GEN-LAST:event_rdArquivoSaidaActionPerformed

    private void txtArquivoSaidaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtArquivoSaidaKeyReleased
        questao.setNomeArquivoSaida(txtArquivoSaida.getText());
        this.modificado = true;
    }//GEN-LAST:event_txtArquivoSaidaKeyReleased

    private void btnAdicionarTesteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarTesteActionPerformed
        TabelasTeste tab = new TabelasTeste((javax.swing.JFrame)this.getParent(), listaTestes.getModel().getSize());
        tab.setVisible(true);
        Teste teste = tab.getTeste();
        if (teste != null) {
            questao.adicionarTeste();
            questao.editarTeste(listaTestes.getModel().getSize(), teste);
            listaTestes.setListData(getVetorTestes());
            listaTestes.setSelectedIndex(0);
            btnRemoverTeste.setEnabled(true);
            btnEditarTeste.setEnabled(true);
            this.modificado = true;
        }
    }//GEN-LAST:event_btnAdicionarTesteActionPerformed
    
    private void btnAdicionarTesteTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarTesteActionPerformed
    	UIManager.put("FileChooser.openDialogTitleText", "Escolha o arquivo de testes");
        File arquivo = null;
        
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int resultado = fc.showOpenDialog(this);
        Janelas.alinharContainer(fc);
        if (resultado == JFileChooser.CANCEL_OPTION) {
            arquivo = null;
        } else {
            arquivo = fc.getSelectedFile();
            try {
                String entradas[] = Arquivos.getEntrada(arquivo);
                String colGabs[] = Arquivos.getColunas(arquivo); 
                String gabaritos[] = Arquivos.getGabaritos(arquivo,colGabs.length);
                
                System.out.println(Arrays.toString(entradas));
                System.out.println(Arrays.toString(colGabs));
                System.out.println(Arrays.toString(gabaritos));
                //System.exit(0);
                
                TabelasTeste tab = new TabelasTeste((javax.swing.JFrame)this.getParent(), listaTestes.getModel().getSize(), entradas, gabaritos,colGabs);
                tab.setVisible(true);
                Teste teste = tab.getTeste();
                if (teste != null) {
                    questao.adicionarTeste();
                    questao.editarTeste(listaTestes.getModel().getSize(), teste);
                    listaTestes.setListData(getVetorTestes());
                    listaTestes.setSelectedIndex(0);
                    btnRemoverTeste.setEnabled(true);
                    btnEditarTeste.setEnabled(true);
                    this.modificado = true;
                }
                
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "O arquivo informado náo é válido",
                        "Arquivo Inválido!", JOptionPane.ERROR_MESSAGE);
            }
            
        }
    }//GEN-LAST:event_btnAdicionarTesteActionPerformed

    private void btnRemoverTesteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoverTesteActionPerformed
        int testeSelecionado = listaTestes.getSelectedIndex();
        
        questao.removerTeste(testeSelecionado);
        listaTestes.setListData(getVetorTestes());
        if (listaTestes.getModel().getSize() == 0) {
            btnRemoverTeste.setEnabled(false);
            btnEditarTeste.setEnabled(false);
        } else {
            listaTestes.setSelectedIndex(0);
        }
        this.modificado = true;
    }//GEN-LAST:event_btnRemoverTesteActionPerformed

    private void btnEditarTesteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarTesteActionPerformed
        int testeSelecionado = listaTestes.getSelectedIndex();
        
        TabelasTeste tab = new TabelasTeste(null, testeSelecionado, questao.getTeste(testeSelecionado));
        tab.setVisible(true);
        if (tab.getSaidaNormal()) {
            questao.editarTeste(testeSelecionado, tab.getTeste());
            this.modificado = true;
        }
    }//GEN-LAST:event_btnEditarTesteActionPerformed

    private void btnAbrirQuestaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbrirQuestaoActionPerformed
        JFileChooser fc = new JFileChooser();
        File arqQuestao = null;
        
        UIManager.put("FileChooser.openDialogTitleText", "Importar Questão");
        fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        Janelas.alinharContainer(fc);
        int resultado = fc.showOpenDialog(this);
        if(resultado == JFileChooser.CANCEL_OPTION){
            return;
        } else {
            arqQuestao = fc.getSelectedFile();
            try {
                GerenciaSerializacao ger = new GerenciaSerializacao();
                questao = ger.desserializarQuestao(arqQuestao);

                rdPadraoEntrada.setSelected(true);
                txtArquivoEntrada.setText("");
                txtArquivoEntrada.setEnabled(false);
                if(questao.getTestes().size() > 0) {
                    btnRemoverTeste.setEnabled(true);
                    btnEditarTeste.setEnabled(true);
                    if(questao.getTeste(0).getNomeArquivoEntrada() != null){
                        rdArquivoEntrada.setSelected(true);
                        txtArquivoEntrada.setText(questao.getTeste(0).getNomeArquivoEntrada());
                        txtArquivoEntrada.setEnabled(true);
                    }
                }

                if(questao.getNomeArquivoSaida() == null) {
                    rdPadraoSaida.setSelected(true);
                    txtArquivoSaida.setText("");
                    txtArquivoSaida.setEnabled(false);
                } else {
                    rdArquivoSaida.setSelected(true);
                    txtArquivoSaida.setText(questao.getNomeArquivoSaida());
                    txtArquivoSaida.setEnabled(true);
                }
                listaTestes.setListData(getVetorTestes());
                listaTestes.setSelectedIndex(0);
                textEnunciado.setText(questao.getEnunciado());
                textGabarito.setText(questao.getGabarito());
            } catch(IOException e){
                JOptionPane.showMessageDialog(this, "O arquivo informado não é válido .ser!", "Arquivo Inválido!",
                        JOptionPane.ERROR_MESSAGE);
            } catch(IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
}//GEN-LAST:event_btnAbrirQuestaoActionPerformed

    private void btnSalvarQuestaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarQuestaoActionPerformed
        try{
            if(salvar())
                this.dispose();
        }catch(IOException e){
            JOptionPane.showMessageDialog(null, "Insira um enunciado","Enunciado vazio!",JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_btnSalvarQuestaoActionPerformed

    private void btnAdicionarEnunciadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarEnunciadoActionPerformed
        UIManager.put("FileChooser.openDialogTitleText", "Escolha do Enunciado");
        File arquivo = null;
        
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int resultado = fc.showOpenDialog(this);
        Janelas.alinharContainer(fc);
        if (resultado == JFileChooser.CANCEL_OPTION) {
            arquivo = null;
        } else {
            arquivo = fc.getSelectedFile();
            try {
                String enunciado = Arquivos.getTextoArquivo(arquivo);
                questao.setEnunciado(enunciado);
                textEnunciado.setText(questao.getEnunciado());
                this.modificado = true;
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "O arquivo informado não é válido",
                        "Arquivo Inválido!", JOptionPane.ERROR_MESSAGE);
            }
            
        }
    }//GEN-LAST:event_btnAdicionarEnunciadoActionPerformed

    private void textEnunciadoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textEnunciadoKeyReleased
        questao.setEnunciado(textEnunciado.getText());
        this.modificado = true;
    }//GEN-LAST:event_textEnunciadoKeyReleased

    private void textGabaritoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textGabaritoKeyReleased
        questao.setGabarito(textGabarito.getText());
        this.modificado = true;
}//GEN-LAST:event_textGabaritoKeyReleased

    private void btnAdicionarGabaritoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarGabaritoActionPerformed
        UIManager.put("FileChooser.openDialogTitleText", "Escolha do Gabarito");
        File arquivo = null;
        
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int resultado = fc.showOpenDialog(this);
        Janelas.alinharContainer(fc);
        if (resultado == JFileChooser.CANCEL_OPTION) {
            arquivo = null;
        } else {
            arquivo = fc.getSelectedFile();
            try {
                String gabarito = Arquivos.getTextoArquivo(arquivo);
                questao.setGabarito(gabarito);
                textGabarito.setText(questao.getGabarito());
                this.modificado = true;
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "O arquivo informado não é válido",
                        "Arquivo Inválido!", JOptionPane.ERROR_MESSAGE);
            }
            
        }
    }//GEN-LAST:event_btnAdicionarGabaritoActionPerformed

    private void rdPadraoEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdPadraoEntradaActionPerformed
        txtArquivoEntrada.setText("");
        txtArquivoEntrada.setEnabled(false);

}//GEN-LAST:event_rdPadraoEntradaActionPerformed

    private void rdArquivoEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdArquivoEntradaActionPerformed
        txtArquivoEntrada.setEnabled(true);
}//GEN-LAST:event_rdArquivoEntradaActionPerformed

    private void txtArquivoEntradaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtArquivoEntradaKeyReleased
    
    }//GEN-LAST:event_txtArquivoEntradaKeyReleased
    

//        private void rdPadraoSaidaActionPerformed(java.awt.event.ActionEvent evt) {
//        txtArquivoSaida.setText("");
//        txtArquivoSaida.setEnabled(false);
//        questao.setNomeArquivoSaida(null);
//        this.modificado = true;
//    }
//
//    private void rdArquivoSaidaActionPerformed(java.awt.event.ActionEvent evt) {
//        txtArquivoSaida.setEnabled(true);
//        questao.setNomeArquivoSaida(txtArquivoSaida.getText());
//        this.modificado = true;
//    }
//
//    private void txtArquivoSaidaKeyReleased(java.awt.event.KeyEvent evt) {
//        questao.setNomeArquivoSaida(txtArquivoSaida.getText());
//        this.modificado = true;
//    }


    private String[] getVetorTestes(){
        ArrayList<Teste> testes = questao.getTestes();
        String[] vetorTestes = new String[testes.size()];
        for (int i = 0; i <= vetorTestes.length - 1; i++) {
            vetorTestes[i] = "Teste " + (i + 1);
        }
        
        return vetorTestes;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JanelaQuestao dialog = new JanelaQuestao();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    private boolean salvar() throws IOException {
        JFileChooser fc = new JFileChooser();
        File arqQuestao = null;
        String nomeArquivoEntrada = new String();
        
        if(textEnunciado.getText().isEmpty())
            throw new IOException();

        if(rdPadraoEntrada.isSelected())
            nomeArquivoEntrada = null;
        else
            nomeArquivoEntrada = txtArquivoEntrada.getText();
            
        
        for(Teste teste:questao.getTestes()){
            teste.setNomeArquivoEntrada(nomeArquivoEntrada);
        }


        UIManager.put("FileChooser.openDialogTitleText", "Exportar Questão");
        fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        Janelas.alinharContainer(fc);
        fc.setSelectedFile(new File("questao.ser"));
        int resultado = fc.showSaveDialog(this);
        if(resultado == JFileChooser.CANCEL_OPTION){
            return false;
        } else {
            arqQuestao = fc.getSelectedFile();
            if(!arqQuestao.getAbsolutePath().endsWith(".ser"))
                arqQuestao = new File(arqQuestao.getAbsolutePath() + ".ser");
            if(arqQuestao.exists()){
                int opcao = JOptionPane.showConfirmDialog(null, "Deseja sobreescrever?","Questão existente",JOptionPane.YES_NO_OPTION);
                if(opcao == JOptionPane.NO_OPTION)
                    return false;
            }
        }
        
        try{
            GerenciaSerializacao ger = new GerenciaSerializacao();
            ger.serializarQuestao(questao, arqQuestao);
        }catch(IndexOutOfBoundsException e){
            e.printStackTrace();
        }
        modificado = false;
        return true;
    }
     
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAbrirQuestao;
    private javax.swing.JButton btnAdicionarEnunciado;
    private javax.swing.JButton btnAdicionarGabarito;
    private javax.swing.JButton btnAdicionarTeste;
    private javax.swing.JButton btnAdicionarTesteTXT;
    private javax.swing.JButton btnEditarTeste;
    private javax.swing.JButton btnRemoverTeste;
    private javax.swing.JButton btnSalvarQuestao;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList listaTestes;
    private javax.swing.JRadioButton rdArquivoEntrada;
    private javax.swing.JRadioButton rdArquivoSaida;
    private javax.swing.JRadioButton rdPadraoEntrada;
    private javax.swing.JRadioButton rdPadraoSaida;
    private javax.swing.JTextArea textEnunciado;
    private javax.swing.JTextArea textGabarito;
    private javax.swing.JTextField txtArquivoEntrada;
    private javax.swing.JTextField txtArquivoSaida;
    // End of variables declaration//GEN-END:variables
    
}